#include "tcpclient.h"
#include "ui_tcpclient.h"
#include "protocol.h"
#include "opewidget.h"

TcpClient::TcpClient(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TcpClient)
{
    ui->setupUi(this);
    this->resize(450,350);
//    QLineEdit* line = new QLineEdit(this);
//    line->setText("byld");
    loadConfig();
    //连接服务器，如果连接成功，服务器会发送connected信号
    m_tcpSocket.connectToHost(QHostAddress(m_strIP),m_usPort);
    //将槽函数和信号绑定，(谁发出的信号，什么信号，谁来处理，如何让处理)
    connect(&m_tcpSocket,SIGNAL(connected()),this,SLOT(showConnect()));
    connect(&m_tcpSocket,SIGNAL(readyRead()),this,SLOT(recvmsg()));

}

TcpClient::~TcpClient()
{
    delete ui;
}

void TcpClient::loadConfig()
{
    QFile file(":/client.txt");
    if(file.open(QIODevice::ReadOnly))
    {
        QByteArray byData=file.readAll();
        QString strData=byData.toStdString().c_str();
        //qDebug()<<strData;
        file.close();
        strData.replace("\r\n"," ");
        QStringList strList = strData.split(" ");
        m_strIP = strList.at(0);
        m_usPort = strList.at(1).toUShort();
        //qDebug()<<m_strIP<<" "<<m_usPort;
    }
    else
    {
        QMessageBox::critical(this,"open config","open config failed");
    }
}

TcpClient &TcpClient::getinstance()
{
    static TcpClient instance;
    return instance;
}

QTcpSocket &TcpClient::getTcpSocket()
{
    return m_tcpSocket;
}

//自定义槽函数实现
void TcpClient::showConnect()
{
    QMessageBox::information(this,"连接服务器","连接成功");
}

#if 0
void TcpClient::on_button_sent_clicked()
{
    QString strMsg = ui->caMSG->text();
    qDebug()<<strMsg;
    if(!strMsg.isEmpty())
    {
        PDU* pdu = mkPDU(sizeof(strMsg));
        memcpy(pdu->caMsg,strMsg.toStdString().c_str(),strMsg.size());
        pdu->uiMsgType=8888;
        qDebug()<<pdu->caMsg;
        m_tcpSocket.write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu=NULL;
    }
    else
    {
        QMessageBox::critical(this,"消息发送","不能发送空消息");
    }

}
#endif

void TcpClient::on_button_login_clicked()
{
    QString name=ui->lineName->text();
    QString pwd= ui->linePwd->text();
    if(!name.isEmpty()&&!pwd.isEmpty())
    {
        //创建登录数据pdu
        PDU* pdu = mkPDU(0);
        pdu->uiMsgType=ENUM_MSG_TYPE_LOGIN_REQUEST;
        //将用户名和密码写入到pdu中
        strncpy(pdu->odata,name.toStdString().c_str(),32);
        strncpy(pdu->odata+32,pwd.toStdString().c_str(),32);
        m_tcpSocket.write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu=NULL;

    }
    else
    {
        QMessageBox::critical(this,"登录","账号密码不能为空");
    }
}

void TcpClient::on_button_regist_clicked()
{
    QString name=ui->lineName->text();
    QString pwd= ui->linePwd->text();
    if(!name.isEmpty()&&!pwd.isEmpty())
    {
        //创建注册数据pdu
        PDU* pdu = mkPDU(0);
        pdu->uiMsgType=ENUM_MSG_TYPE_REGIST_REQUEST;
        //将用户名和密码写入到pdu中
        strncpy(pdu->odata,name.toStdString().c_str(),32);
        strncpy(pdu->odata+32,pwd.toStdString().c_str(),32);
        m_tcpSocket.write((char*)pdu,pdu->uiPDULen);
        free(pdu);
        pdu=NULL;

    }
    else
    {
        QMessageBox::critical(this,"注册","账号密码不能为空");
    }
}

void TcpClient::on_button_cancel_clicked()
{

}

void TcpClient::recvmsg()
{
    uint uiPDULen=0;
    m_tcpSocket.read((char*)&uiPDULen,sizeof(uint));
    uint uiMsgLen =uiPDULen-sizeof(PDU);
    PDU *pdu=mkPDU(uiMsgLen);
    m_tcpSocket.read((char*)pdu+sizeof(uint),uiPDULen-sizeof(uint));
    //qDebug()<<pdu->uiMsgType<<" "<<pdu->uiMsgLen<<" "<<pdu->caMsg;
    switch(pdu->uiMsgType)
    {
        case ENUM_MSG_TYPE_REGIST_RESPOND:
        {
            //
            if(0==strcmp(pdu->odata,REGIST_OK))
            {
                QMessageBox::information(this,"注册","注册成功");
            }
            else if(0==strcmp(pdu->odata,REGIST_FAILED))
            {
                QString tip(REGIST_FAILED);
                QMessageBox::information(this,"注册",tip);
            }
            break;
        }
        case ENUM_MSG_TYPE_LOGIN_RESPONSE:
        {
            //
            if(0==strcmp(pdu->odata,LOGIN_OK))
            {
                QMessageBox::information(this,"登录","登录成功");
                OpeWidget::getinstance().show();
                this->hide();
            }
            else if(0==strcmp(pdu->odata,LOGIN_FAILED))
            {
                QString tip(LOGIN_FAILED);
                QMessageBox::information(this,"登录",tip);
            }
            break;
        }
    case ENUM_MSG_TYPE_ALL_ONLINE_USER_RESPONSE:
    {
        //2、收到数据并显示，显示在online界面上的用户列表上
        OpeWidget::getinstance().getFriend()->ShowAllUser(pdu);
        break;
    }
    case ENUM_MSG_TYPE_SEARCH_USER_RESPONSE:
    {
        int ret = 0;
        memcpy(&ret,pdu->odata,sizeof(int));
        if(ret == 1)
        {
            QMessageBox::information(this,"搜索","该用户在线");
        }
        else if(ret == 0)
        {
            QMessageBox::information(this,"搜索","该用户不在线");
        }
        else if(ret == -1)
        {
            QMessageBox::information(this,"搜索","该用户不存在");
        }
        break;
    }
    default:
        break;
    }

}
